package com.dyson;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @Author: Wy
 * @Email: DysonWy@foxMail.com
 * @Time: 2020/2/27 14:04
 * @Description:
 */
public class ZipFiles {
    static String sourceFile = "I:\\动力节点\\测试解压\\压缩部分";
    static String tagPath = "I:\\动力节点\\测试解压\\解压部分";

    public static void main(String[] args) {

        unZipPath(sourceFile, tagPath);
    }

    private static void unZipPath(String sourceFile, String tagPath) {
        File file = new File(sourceFile);
        if (!file.exists()) {
            return;
        }
        File[] files = file.listFiles();
        if (files == null || files.length == 0) {
            return;
        }
        for (File childFile : files) {
            if (childFile.isDirectory()) {
                System.out.println(childFile.getAbsolutePath());
                unZipPath(childFile.getAbsolutePath(), tagPath);
            }

        }
        //unZipFlie(), tagPath);
    }

    private static void unZipFlie(File sourceFile, String tagPath) {
        if (!sourceFile.exists()) {
            return;
        }
        try {
            ZipFile zipFile = new ZipFile(sourceFile, Charset.forName("GBK"));
            String zipFileName = zipFile.getName();
            int pointIndex = zipFileName.lastIndexOf('.');
            String suffix = zipFileName.substring(pointIndex + 1);
            String prefix = zipFileName.substring(0, pointIndex);
            String fileName = prefix.substring(prefix.lastIndexOf('\\') + 1);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                String name = entry.getName();
                InputStream inputStream = zipFile.getInputStream(entry);
                String outPath = (tagPath + "\\" + name);
                //File file = new File(outPath);
                FileOutputStream out = new FileOutputStream(outPath);
                byte[] buf1 = new byte[1024];
                int len;
                while ((len = inputStream.read(buf1)) > 0) {
                    out.write(buf1, 0, len);
                }
                out.close();
                inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
